﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

namespace WebApp.admin.warehouse
{
    public partial class warehouseGoodsInventoryList : BaseAdmin
    {
        protected string kw = "";
        protected string order = "id";
        protected int ot = 0;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();
        tbCardType oArea;

        protected int t = 0;
        protected int total = 0;

        tbWarehouseGoodsInventory oWarehouseGoodsInventory;

        // Methods
        private void CardTypeBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                this.pl.TblName = "tbWarehouseGoodsInventory";
                this.pl.StrGetFields = "  id, theme, inventoryTime,(select warehouseName from tbWarehouse where id= tbWarehouseGoodsInventory.warehouseId) as warehouseName, inventoryAdminId, inventoryStates,convert(nvarchar(10),inventoryRemark,121) as inventoryRemark  "
                                      + ", (select AdminName  from tbAdminLogin where adminId = tbWarehouseGoodsInventory.inventoryAdminId) as adminName,convert(nvarchar(10),createTime,121) as createTime";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                if (! string.IsNullOrEmpty(Request.QueryString["gid"]))
                {
                    this.pl.StrWhere = "goodsId=" + Request.QueryString["gid"];
                }

                if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
                {
                      this.kw = base.Request.QueryString["kw"];
                      string sBefor = string.IsNullOrEmpty(this.pl.StrWhere) ? "" : this.pl.StrWhere + " and ";
                      this.pl.StrWhere = sBefor + " warehouseId in (select id from tbWarehouse where " + base.Request.QueryString["t"] + " like '%" + this.kw + "%')";
             
                }

                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnDelete.Visible = false;
                }
                this.rptWarehouseGoodsInventory.DataSource = pageList.DefaultView;
                this.rptWarehouseGoodsInventory.DataBind();
            }
            catch
            {
            }
        }



        protected void warehouseGoodsInventoryList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (((System.Data.DataRowView)e.Item.DataItem)["inventoryStates"].ToString() == "0")
                {
                    e.Item.FindControl("divEdit").Visible = true;
                    e.Item.FindControl("divDeleted").Visible = true;
                    e.Item.FindControl("divSubmit").Visible = true;
                }
                else
                {
                    e.Item.FindControl("divEdit").Visible = false;
                    e.Item.FindControl("divDeleted").Visible = false;
                    e.Item.FindControl("divSubmit").Visible = false;
                }
              
                  
              
            }
         

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string str = base.Request.Form["Item"];
                if (str.Contains(","))
                {
                    Array array = str.Split(new char[] { ',' });
                    foreach (string str2 in array)
                    {
                        BaseFactory.theWarehouseGoodsInventoryDetail.DeleteByInventoryId(int.Parse(str2));
                        BaseFactory.theWarehouseGoodsInventory.Delete(int.Parse(str2));
                    }

                    Response.Write("<script> alert('删除成功！');window.location.href='warehouseGoodsInventoryList.aspx';</script>");
                }
            }
            catch
            {
                base.Alert("删除失败！");
            }
            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }

   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehousesInventory);

                this.DeleteItem();
                this.SubmitItem();
                if (base.Request.QueryString["t"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "ssf", "<script type=\"text/javascript\">selectIsExitItem(\"selectWarehouse\",\"" + base.Request.QueryString["t"] + "\");</script>");
                }

                if (base.Request.QueryString["ps"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
                }
                this.btnDelete.Attributes.Add("disabled", "disabled");
                this.CardTypeBind();
            }
        }

        void DeleteItem()
        {
            if (Request.QueryString["action"] == "del" && Request.QueryString["id"] != null)
            {
                BaseFactory.theWarehouseGoodsInventoryDetail.DeleteByInventoryId(int.Parse(Request.QueryString["id"]));
                BaseFactory.theWarehouseGoodsInventory.Delete(int.Parse(Request.QueryString["id"]));
               base.Alert("删除成功！");
            }
    
        }

        void SubmitItem()
        {
            if (Request.QueryString["action"] == "sub" && Request.QueryString["id"] != null)
            {
               oWarehouseGoodsInventory = BaseFactory.theWarehouseGoodsInventory.GetModel(int.Parse(Request.QueryString["id"]));
                if(oWarehouseGoodsInventory != null)
                {
                    oWarehouseGoodsInventory.inventoryStates = 1;
                   if(BaseFactory.theWarehouseGoodsInventory.Update(oWarehouseGoodsInventory))
                   {
                       Response.Write("<script> alert('提交成功！');window.location.href='warehouseGoodsInventoryList.aspx';</script>");
                   }else
                   {
                       base.Alert("提交失败！");
                   }

                }
            }
        }
    

    }
}